﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Web.Utility;
using Portal.Web.Model;
using System.Data;
using Portal.Xml.Utility;
using Portal.Workflow.Bussiness;
using Portal.Workflow.Model;
using Portal.Systems.Model;
using Portal.Workflow.Utilities;
using Portal.Workflow.Render;
using Portal.Utility;
using Portal.Workflow.Render.Template;
using Portal.Admin.Home.Render.Template;
using System.Web;

namespace Portal.Admin.Screen.Render
{
    public class HomeRenderBll : AdminHomeRenderTemplate
    {
        public override string ServiceId
        {
            get
            {
                return "DefaultAdminHomeRenderBll";
            }
        }


        public override AjaxOut Draw(string SiteId, RenderInfoCls ORenderInfo)
        {
            return Draw(SiteId);
        }

        static WebFolderCls[] ReadingWebFolders(SiteParam OSiteParam, string SiteId, string Folder)
        {
            //SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId)           

            string XmlFile = WebConfig.GetWebPartLayout(OSiteParam);
            if (string.IsNullOrEmpty(XmlFile))
            {
                throw new Exception("Chưa xác định cấu hình WebPartLayout trong file cấu hình " + OSiteParam.XmlConfigFile);
            }
            if (new System.IO.FileInfo(XmlFile).Exists == false)
            {
                throw new Exception(XmlFile + " không tồn tại trong file cấu hình " + OSiteParam.XmlConfigFile);
            }
            DataSet ds = XmlUtility.GetDataSetFromXmlFile(XmlFile);
            DataView dv = null;
            if (string.IsNullOrEmpty(Folder))
            {
                dv = new DataView(ds.Tables["WebFolder"], "[Visible]=1 and ParentFolderId=''", "SortIndex ASC", DataViewRowState.CurrentRows);
            }
            else
            {
                dv = new DataView(ds.Tables["WebFolder"], "[Visible]=1 and ParentFolderId='" + Folder + "'", "SortIndex ASC", DataViewRowState.CurrentRows);
            }
            WebFolderCls[] WebFolders = WebFolderParser.ParseFromDataView(dv);
            return WebFolders;
        }

        public static bool IsChecked(string SiteId, RoleMenuCls[] RoleMenus, string WebFolderId)
        {
            Portal.Systems.Model.UserCls OUser = WebSessionUtility.GetCurrentLoginUser();
            if (OUser.IsAdmin == 1 || OUser.IsSystemAdmin) return true;
            for (int iIndex = 0; iIndex < RoleMenus.Length; iIndex++)
            {
                if (RoleMenus[iIndex].WebFolderId.Equals(WebFolderId))
                {
                    return true;
                }
            }
            return false;
        }


        static WebFolderCls FindWebFolder(string SiteId, string Folder)
        {
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string XmlFile = WebConfig.GetWebPartLayout(OSiteParam);
            DataSet ds = XmlUtility.GetDataSetFromXmlFile(XmlFile);
            DataView dv = new DataView(ds.Tables["WebFolder"], "Id='" + Folder + "'", "SortIndex ASC", DataViewRowState.CurrentRows);
            if (dv.Count == 0)
            {
                return null;
            }
            WebFolderCls WebFolder = WebFolderParser.ParseFromDataRowView(dv[0]);
            return WebFolder;
        }


        public static AjaxOut DrawFolder(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                //SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                SiteId = HttpUtility.HtmlEncode(SiteId);

                SiteParam OSiteParam = null;
                try
                {
                    OSiteParam = Utility.CreateSiteParam(SiteId, HttpUtility.HtmlEncode(WebConfig.XmlSiteConfigFile));
                }
                catch { }
                if (OSiteParam == null)
                {
                    OAjaxOut.HtmlContent = "<div class=formLogin>" + SiteId + " not found in siteconfig.xml" + "</div>";
                    return OAjaxOut;
                }
                
                string folder = (string)WebEnvironments.Request(SiteId, "folder");
                WebFolderCls[] WebFolders = ReadingWebFolders(OSiteParam, SiteId, folder);


                string RoleId = WebSessionUtility.GetCurrentLoginUser().RoleId;
                RoleMenuCls[] RoleMenus = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRoleMenus(SiteId, RoleId);

                string WebPartLayoutIconRoot = WebConfig.GetWebPartLayoutIconRoot(OSiteParam);
                string SubTitle = "";
                string Title = "CÁC CHỨC NĂNG CỦA HỆ THỐNG QUẢN LÝ";
                WebFolderCls ActiveFolder = FindWebFolder(SiteId, folder);
                if (ActiveFolder != null)
                {
                    Title = "CHỨC NĂNG - " + ActiveFolder.Title.ToUpper();
                    SubTitle = ActiveFolder.Description;
                }
                if (string.IsNullOrEmpty(folder))
                {
                    SubTitle = "Danh sách các chức năng trong hệ thống cổng!";
                }
                bool Found = false;
                string Html =
                   "<div class=FormFunction>\r\n" +
                   "    <h3>" + Title + "</h3>\r\n" +
                   "    <div class=CssSubTitle>" + SubTitle + "</div>\r\n" +
                   "    <div class=body>\r\n" +
                   "        <div class=CssNavigator>\r\n" +
                   "        </div>\r\n" +
                   "        <div class=divInfo>\r\n" +
                   "            <ul>\r\n";
                for (int iIndex = 0; iIndex < WebFolders.Length; iIndex++)
                {
                    bool Allow = IsChecked(SiteId, RoleMenus, WebFolders[iIndex].Id);
                    if (Allow)
                    {
                        string ImageUrl = WebPartLayoutIconRoot + "/" + WebFolders[iIndex].IconUrl;
                        string Aspx = WebFolders[iIndex].Aspx;
                        if (WebFolders[iIndex].Id.Equals("back"))
                        {
                            if (!string.IsNullOrEmpty(WebFolders[iIndex].Aspx))
                            {
                                Aspx = WebFolders[iIndex].Aspx;
                            }
                            else
                            {
                                Aspx = "folder.aspx";
                            }
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(Aspx))
                            {
                                Aspx = "folder.aspx";
                                Aspx = Aspx + "?folder=" + WebFolders[iIndex].Id;
                            }
                            else
                            {
                                if (WebFolders[iIndex].IsFolder == 0)
                                {
                                    Aspx = WebFolders[iIndex].Aspx;
                                }
                            }
                        }
                        Html +=
                               "    <li onclick=\"window.open('" + Aspx + "','_self');\">\r\n" +
                               "        <div><img style=\"height:60px;\" src=\"" + ImageUrl + "\"></div>\r\n" +
                               "        <div alt=\"" + WebFolders[iIndex].Description + "\" title=\"" + WebFolders[iIndex].Description + "\">" + WebFolders[iIndex].Title + "</div>\r\n" +
                               "    </li>\r\n";
                        Found = true;
                    }
                }

                Html +=
                   "            </ul>\r\n";
                if (!Found)
                {
                    Html += "<div class=CssNoData>Bạn không có quyền truy cập vào bất cứ chức năng nào. Liên hệ quản trị hệ thống</div>\r\n";
                }
                Html +=
                   "        </div>\r\n" +
                   "    </div>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteId = HttpUtility.HtmlEncode(SiteId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string AdminHomeRenderServiceId = WebConfig.GetConfigFromXml(OSiteParam, "AdminHomeRenderServiceId");
                IAdminHomeRender FoundAdminHomeRender = AdminHomeRenderUtility.Find(SiteId, AdminHomeRenderServiceId);
                if (FoundAdminHomeRender != null)
                {
                    OAjaxOut = FoundAdminHomeRender.Draw(SiteId, null);
                }

            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public AjaxOut ServerSideDeleteUserQuickLink(string SiteId, string UserQuickLinkId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).DeleteUserQuickLink(SiteId, UserQuickLinkId);
                OAjaxOut.HtmlContent = ServerSideReadingUserQuickLink(SiteId).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        static AjaxOut ServerSideReadingUserQuickLink(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                UserQuickLinkCls[] UserQuickLinks = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingUserQuickLinks(SiteId, WebSessionUtility.GetCurrentLoginUser().UserId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonRootUrl = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                            "<ul>\r\n";
                for (int iIndex = 0; iIndex < UserQuickLinks.Length; iIndex++)
                {
                    Html +=
                            "     <li><a href=\"" + UserQuickLinks[iIndex].LinkUrl + "\">" + UserQuickLinks[iIndex].Subject + "</a><img src=\"" + ButtonRootUrl + "/delete.png\" style=\"width:20px;float:right;cursor:pointer;margin-right:4px;\" alt=\"Xóa shortcut\" title=\"Xóa shortcut\" onclick=\"javascript:ClientDeleteShortCut('" + UserQuickLinks[iIndex].UserQuickLinkId + "');\"></li>\r\n";
                }
                Html +=
                    "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }

    }
}
